package com.jx.driving.protocol.terminal.codec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;

import com.jx.driving.util.ByteUtils;

/**
 * 32位dword读写器
 * 
 * @author shenliuyang
 *
 *         2016年8月1日上午10:10:35
 */
public class DwordFieldReaderWriter extends FieldReaderWriter {

	public DwordFieldReaderWriter(Field field, int size, int order) {
		super(field, 4, order);
	}

	@Override
	public void read(InputStream in, Object to) throws IOException {
		byte[] data = new byte[4];
		in.read(data);
		long ret = ByteUtils.byteToLong(data);
		wrtietoField(to, ret);
	}

	@Override
	public void write(OutputStream out, Object from) throws IOException {
		Number i = (Number) readFromField(from);
		if (i == null) {
			i = 0;
		}
		ByteBuffer bb = ByteBuffer.allocate(4);
		bb.putInt(i.intValue());
		out.write(bb.array());
	}
}